package http

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"mall-portalv2/conf"
	"mall-portalv2/global"
	"mall-portalv2/internal/server/middleware"
	"mall-portalv2/internal/service"
)

/**
* Author: joker
* TODO: test
* Date: 2022/8/16
* Time: 上午9:29
**/

var (
	svc *service.Service
)

// New a gin server.
func New(s *service.Service) *gin.Engine {
	engine := gin.Default()
	// 跨域配置
	engine.Use(middleware.Cors())
	gin.SetMode(gin.ReleaseMode)
	if conf.Config.System.Debug {
		gin.SetMode(gin.DebugMode)
		engine.Static("/logs", "./logs")
	}
	engine.Use(middleware.OptionOk())

	engine.MaxMultipartMemory = 1 << 20 * 100 // 100MB

	// 设置信任代理
	engine.SetTrustedProxies([]string{"localhost", "127.0.0.1"})

	svc = s

	initRouter(engine, s)

	if conf.Config.System.Tls {
		if err := engine.RunTLS(fmt.Sprintf(":%d", conf.Config.System.Port), "/cert/server.crt", "/cert/server.key"); err != nil {
			global.Log.Panic("Server start filed", zap.Error(err))
		}
	} else {
		if err := engine.Run(fmt.Sprintf(":%d", conf.Config.System.Port)); err != nil {
			global.Log.Panic("Server start filed", zap.Error(err))
		}
	}

	return engine
}

func initRouter(e *gin.Engine, s *service.Service) {
	go s.ReadRabbitMqMessage()

	//pprof.Register(e)

	e.Static("/static/", "./assets")

	e.Any("/healthcheck.html", healthCheck)

	//tmpl := template.New("")
	//templ := template.Must(tmpl.ParseFS(AdminF, "templates/*/*.tmpl"))
	//e.SetHTMLTemplate(templ)

	callbackRouter := e.Group("/callback")
	{
		callbackRouter.POST("/alipay", CallBackAliPayHandlerFunc)
		callbackRouter.POST("/wechat", CallBackWechatPayHandlerFunc)
		callbackRouter.POST("/temp", CallBackTempHandlerFunc)
	}

	publicRouter := e.Group("/public")
	{
		publicRouter.GET("/refresh/time", publicRefreshTimeHandlerFunc)
	}

	ssoRouter := e.Group("/sso")
	{
		privateRouter := ssoRouter.Group("/")
		privateRouter.Use(middleware.Jwt(svc))
		{
			privateRouter.GET("/info", SsoJiaChaoUserInfo)
		}
		ssoRouter.GET("/refresh/info", SsoJiaChaoUserLogin)
	}

	cartRouter := e.Group("/cart")
	cartRouter.Use(middleware.Jwt(svc))
	//cartRouter.Use(middleware.Logs(s))
	{
		// 添加商品到购物车
		cartRouter.POST("/add", CartAddHandlerFunc)
		// 清空当前会员的购物车
		cartRouter.POST("/clear", CartClearHandlerFunc)
		// 删除购物车中的指定商品
		cartRouter.POST("/delete", CartDeleteHandlerFunc)
		// 获取购物车中指定商品的规格,用于重选规格
		cartRouter.GET("/getProd/:productId", CartGetProdHandlerFunc)
		// 获取当前会员的购物车列表
		cartRouter.GET("/list", CartListHandlerFunc)
		// 获取当前会员的购物车列表,包括促销信息
		cartRouter.GET("/list/promotion", CartListHandlerFunc)
		// 修改购物车中商品的规格
		cartRouter.POST("/update/attr", CartUpdateAttrHandlerFunc)
		// 修改购物车中指定商品的数量
		cartRouter.GET("/cart/update/quantity", CartUpdateQuantityHandlerFunc)
	}

	orderRouter := e.Group("/order")
	orderRouter.Use(middleware.Jwt(svc))
	{
		//// 根据购物车信息生成确认单
		orderRouter.POST("/crowdFunding/generateConfirmOrder", OrderConfirmOrderHandlerFunc)
		//// 生成正式订单
		orderRouter.POST("/crowdFunding/sureOrder", OrderGeneratePayOrderHandlerFunc)
		//// 修改订单
		orderRouter.POST("/crowdFunding/updateOrder", OrderUpdateOrderHandlerFunc)
		//// 生成支付接口
		orderRouter.POST("/pay", OrderPayHandlerFunc)
		//// 取消单个超时订单
		//orderRouter.POST("/cancelOrder", OrderAddHandlerFunc)
		//// 自动取消超时订单
		//orderRouter.POST("/cancelTimeOutOrder", OrderClearHandlerFunc)
		//// 用户取消订单
		orderRouter.GET("/cancelUserOrder", OrderCancelHandlerFunc)
		//// 用户确认收货
		orderRouter.GET("/confirmReceiveOrder", OrderConfirmReceiveHandlerFunc)
		//// 用户删除订单
		orderRouter.GET("/deleteOrder", OrderDeleteHandlerFunc)
		//// 根据ID获取订单详情
		orderRouter.GET("/detail/:orderId", OrderDetailHandlerFunc)
		//// 根据购物车信息生成确认单
		//orderRouter.POST("//generateConfirmOrder", OrderUpdateAttrHandlerFunc)
		//// 根据购物车信息生成订单
		//orderRouter.GET("/generateOrder", OrderUpdateQuantityHandlerFunc)
		//// 按状态分页获取用户订单列表
		orderRouter.GET("/list", OrderListHandlerFunc)
		//// 按状态分页获取用户订单列表
		orderRouter.POST("/read", OrderListReadHandlerFunc)
		//// 用户支付成功的回调
		//orderRouter.GET("/paySuccess", OrderUpdateQuantityHandlerFunc)
	}
}
